
####  V  appendix - Table A6 : ---------------------------------------

# function for learning of bins bounds from decedent 

# function table 2 - start with one row 
set_qauntile <- function(data, num_of_bins=20) {
  qauntile_D<- 
    data[died_first_year==T,
         .(bin_num = 1:num_of_bins,
           upper_bound = quantile(prob_for_report,
                                  seq(1/num_of_bins,1,1/num_of_bins) ))][
                                    num_of_bins, upper_bound:=1
                                    ]
  qauntile_D
}

table_2_func <- function( name = "Total",
                          data_filterd=make_slice_cost_month(dt_cost_for_exhibits_cancer_month),     # person - month cst aggregeted data 
                          data_probs = build_dyn_new() , 
                          type_phat_zero = T,        # type of phat - initial or dynamic 
                          type_binning_level =  T,     # level/distribution binning
                          type_reweight_oneway = T,    # oneway or twoway 
                          decedent_weihgted   = F,
                          parm_month = 30,  # days in month 
                          parm_bins = 0.1,  # size of bin
                          num_of_bins=10){  # num of bins - quantile
  
  # 0) add phat
  if (type_phat_zero ==T) {
    # replace dynamic  prob with inital prob
    dyn_all_prob_temp <- merge(x = data_probs[,-c("prob_for_report","CNRS_topo_main_groups")],
                               y = data_probs[month_from_index == 0,
                                                c("id_var","prob_for_report")],
                               by = "id_var",
                               all.x = TRUE,
                               all.y = FALSE)
    
  } else { # keep dynamic phats
    dyn_all_prob_temp <- data.table::copy(data_probs[,-c("CNRS_topo_main_groups")])
  }
  
  # merge phats and cost data : 
  raw_data_table2 <- merge(x = dyn_all_prob_temp,
                           y = data_filterd,
                           by.x  = c("id_var" ,"month_from_index"),
                           by.y = c("id_var" ,"months_after_dx_0" ),
                           all.x = TRUE,
                           all.y = FALSE)
  
  
  # after merge month-cost data into phats - missing represent zero cost 
  raw_data_table2[is.na(actual_cost),actual_cost:=0]
  
  # calculate how many days lived every month (for decedent)
  raw_data_table2[,days_lived := ifelse(full_month_to_death==0,
                                        as.numeric(difftime(DMG_date_of_death_XX,
                                                            S_index_date_XX , 
                                                            units = "days"))+1 ,
                                        30)][
                                          is.na(full_month_to_death), days_lived:=30][
                                            days_lived>30,days_lived:=30 ]
  raw_data_table2[,group := as.factor(ifelse(died_first_year==F,"Survivor","Decedent"))]
  
  # 2) define bins and aggregate bins
  if (type_binning_level == T) {
    raw_data_table2[,bins := plyr::round_any(prob_for_report , parm_bins, floor)]
  }  else { # learn decedent quantile bins:
    
    
    if (type_phat_zero  == T) { 
      
      qauntile_D_person <- set_qauntile(raw_data_table2[month_from_index==0] , num_of_bins)
    } else { # Dynamic phat 
      qauntile_D_person <- set_qauntile(raw_data_table2 , num_of_bins)
    }
    
    #  assign bin (1-20) to phats : 
    raw_data_table2[,bins :=  cut(prob_for_report,
                                  breaks = c(0,qauntile_D_person$upper_bound),
                                  include.lowest =T,
                                  labels = F)]
  }
  
  data_bin <- 
    raw_data_table2[,.(cost=sum(actual_cost)/sum(days_lived)*parm_month,
                       sum_cost= sum(actual_cost),
                       sum_days = sum(days_lived),
                       N_patient = length(unique(id_var)), 
                       N=.N),
                    by= .(bins,
                          group)]
  
  data_bin_time <- 
    raw_data_table2[,.(cost=sum(actual_cost)/sum(days_lived)*parm_month,
                       sum_cost= sum(actual_cost),
                       sum_days = sum(days_lived),
                       N=.N),
                    by= .(bins,
                          time = month_from_index, 
                          group )]
  
  if (type_reweight_oneway == T & type_phat_zero == F) { # reweight on phat - dynamic phat 
    
    data_bin[ ,weights :=  ifelse(N>=10,N,0)/sum( ifelse(N>=10,N,0)), by = group ]
  }  else {  # if inital phat
    data_bin[ ,weights := ifelse(N_patient>=10,N_patient,0)/sum(ifelse(N_patient>=10,N_patient,0)), by = group ]
  }
  
  data_bin_time[ ,weights := ifelse(N>=10,N,0)/sum( ifelse(N>=10,N,0)), by = group ]
  
  # 3) reweight on phat 
  if (type_reweight_oneway == T) {
    data_bin_w <- merge (x= data_bin,
                         y= data_bin[group == "Decedent",
                                     .(bins,
                                       weights_Decedent = weights)],
                         by = "bins" ,
                         all.x = T,
                         all.y = F )
    
  } else { # twoway reweighting - phat and time 
    
    data_bin_w <- merge (x= data_bin_time,
                         y= data_bin_time[group == "Decedent",
                                          .(bins,
                                            time,
                                            weights_Decedent = weights)],
                         by = c("bins","time") ,
                         all.x = T,
                         all.y = F )
  }
  
  data_bin_w[is.na(weights_Decedent),weights_Decedent:=0]
  # 4) claculate simple results and reweighted results :  
  
  results_cost_w <- 
    data_bin_w[,.(cost_weighted  = weighted.mean(cost,weights_Decedent) ),
               by = group]
  
  results_cost <- 
    raw_data_table2[,.(cost=sum(actual_cost)/sum(days_lived)*parm_month),
                    by = group]
  
  # 5) return
  
  if (decedent_weihgted   == F) { # previosly "False" used in initial prediction only: 
    Table2_row <- 
      data.table( Survivor                 = as.numeric(results_cost[group=="Survivor",2] ),
                  Survivor_reweigthed      = as.numeric(results_cost_w[group=="Survivor",2]),
                  Decedent                 = as.numeric(results_cost[group=="Decedent",2])
      )
  } else { # dynamic prediction 
    Table2_row <- 
      data.table( Survivor                 = as.numeric(results_cost[group=="Survivor",2] ),
                  Survivor_reweigthed      = as.numeric(results_cost_w[group=="Survivor",2]),
                  Decedent                 = as.numeric(results_cost_w[group=="Decedent",2])
      )
  }  
  Table2_row[,Difference := Decedent- Survivor_reweigthed  ]
  Table2_row[,Category:=name]
  
  
  data.table::setcolorder(Table2_row,"Category")
  return(list(Table2_row,data_bin_w))
}


make_slice_cost_month<- function(data = dt_cost_for_exhibits_cancer_month) {
  data[,.(actual_cost =sum(actual_cost,na.rm = T)),
       by = .(id_var = id_var,
              index_date = S_index_date_XX,
              months_after_dx_0 = month_from_index)]
}


full_table_2 <- function(filename,
                         intensity_wards = c("oncology","internal_medicine","geriatry","rehabilitation"),
                         ...) {
  full_table <-   rbind(table_2_func(name = "Total", data_filterd=make_slice_cost_month(dt_cost_for_exhibits_cancer_month), ... )[[1]],
                        table_2_func(name = "All Inpatient:",
                                     data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat%like%"Inpatient"]), ... )[[1]],
                        table_2_func(name = "Planned"      , data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Inpatient_Planned"]), ... )[[1]],
                        table_2_func(name = "Unplanned"    , data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Inpatient_Unplanned"]), ... )[[1]],
                        table_2_func(name = "Low Intensity", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat%like%"Inpatient"&profession%in%intensity_wards]), ... )[[1]],
                        table_2_func(name = "High Intensity", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat%like%"Inpatient"&!profession%in%intensity_wards]),... )[[1]],
                        table_2_func(name = "Other Services:",
                                     data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[!main_cat%like%"Inpatient"]), ... )[[1]],
                        table_2_func(name = "Drugs", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Drugs"]), ... )[[1]],
                        table_2_func(name = "Outpatient", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Outpatient"]), ... )[[1]],
                        table_2_func(name = "Imaging", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Imaging"]), ... )[[1]],
                        table_2_func(name = "Other", data_filterd = make_slice_cost_month(dt_cost_for_exhibits_cancer_month[main_cat=="Others"]), ... )[[1]]
  )
  
  orig_full<-copy(full_table)
  
  full_table[,sh_total := format(round((Difference/full_table[1]$Difference)*100 ,1 ),nsmall = 1)]
  
  full_table[, c(2,3,4,5) :=  lapply(.SD,scales::comma,accuracy=1), .SDcols = c(2,3,4,5)]
  
  full_table[c(3:6,8:11), Category:=paste0("~~",Category)]
  
  
  invisible(Hmisc::latex(
    full_table,
    file = paste0("ave_monthly_cost_",filename,".tex"),
    center = 'centering',
    n.cgroup = c(1,2, 1 ,2 ),
    cgroup   = c("","Survivor", "Decedent", "Difference"),
    extracolheads = c("","(1)", "(2)", "(3)", "(4)","(5)"),
    
    colheads = c("\\thead{Category}",
                 "\\thead{Unweighted}",
                 "\\thead{Reweighted by\\\\Decedent Risk}", 
                 "",
                 "\\thead{Decedent -\\\\ Survivor\\\\(Reweighted)}",
                 "\\thead{Percent of \\\\ Total Difference}"),
    rowname =NULL,
    col.just = c("l", rep.int("r", 5)), 
    extracolsize = "normalsize"
  ))
  
  data_for_figure <-table_2_func(name = "Total", data_filterd=dt_mc, ... )[[2]]
  return(list(full_table,orig_full,data_for_figure))
}



